from dataStore import mainStore
from common import md5
from config import missionConf
from _thread import start_new_thread as createThread
from webCommon import webFetch
from fileOperate import writeToFileRaw

class RepeatGrabber():
    def __init__(self, sdsId): # sdsId for subStoreId
        print("Entered RepeatGrabber.")
        self.ds = mainStore.getSubStore(sdsId)
        print(missionConf["mission"])
        self._conf = missionConf["mission"][str(sdsId)]["config"]

    def start(self):
        print("RepeatGrabber.start Entered.")
        self.threadRunning = [False] * self._conf["thread"]
        self.running = True
        for i in range(self._conf["thread"]):
            createThread(self._grabThread, (i,))

    def stop(self):
        self.running = False

    def runningThreadCount(self):
        return self.threadRunning.count(True)

    def _grabThread(self, threadId):
        self.threadRunning[threadId] = True
        print("Thread", threadId, "start running.")
        try:
            while self.running:
                stt, ret = webFetch(self._conf["url"])
                if not stt: # Fetch failed
                    pass # ignore it
                _md5 = md5(ret)
                if _md5 in self.ds.md5:
                    continue
                print("md5 =", _md5, "has been saved!")
                self.ds.md5.append(_md5)
                writeToFileRaw(self._conf["path"] + "/" + _md5 + "." + self._conf["suffix"], ret)
        finally:
            print("Thread", threadId, "stop running.")
            self.threadRunning[threadId] = False

    def onRunning(self):
        return self.running
